const { promisify } = require( "util" );
const downloadGitRepo = promisify( require( "download-git-repo" ) );
const path = require( "path" );
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const ora = require("ora");
class Creator {
	constructor ( project ) {
		this.project = project;
		this.templateZip = "direct:https://github.com/sjh612/vue-demo/archive/refs/heads/master.zip";
		this.templateUrl = "direct:https://github.com/sjh612/vue-demo.git";
		this.isGitClone = false;
	}

	async create () {
		const spinner = ora( "开始创建");
		spinner.start()
		try {
			await this.download( this.templateZip, this.project );
			spinner.succeed("模板下载完成");
			console.log("安装依赖中。。。")
		// 	安装依赖
			process.chdir(path.join(process.cwd(), this.project));
		// 	使用npm 安装依赖
			const { stdout, stderr } = await exec('npm install');
			if (stderr) {
				spinner.fail("依赖安装失败"+stderr);
			} else {
				spinner.succeed("依赖安装成功");
			}

		} catch ( error ) {
			spinner.fail("发生错误："+error);
		}
	}

	async download ( template, project ) {
		try {
			await downloadGitRepo( template, project );
		} catch ( error ) {
			throw error;
		}
	}
}

module.exports = Creator;
